package tau.adx.common.adn.generators;

import java.util.Collection;
import java.util.LinkedList;
import java.util.Random;

import tau.adx.common.adn.AdNetwork;
import tau.adx.common.adn.impl.SimpleAdNetwork;
import tau.adx.common.ads.Ad;
import tau.adx.common.ads.properties.AdType;
import tau.adx.common.generators.GenericGenerator;

/**
 * A naive implementation of the {@link GenericGenerator} interface. Randomizes
 * each characteristic of an {@link AdNetwork}.
 * 
 * @author greenwald
 * 
 */
public class SimpleAdNetworkGenerator implements GenericGenerator<AdNetwork> {

	/**
	 * Ad network name length. Was chosen arbitrary.
	 */
	private static final int AD_NETWORK_NAME_LENGTH = 10;
	/**
	 * An instance of the {@link Random} class.
	 */
	private static final Random random = new Random();

	/**
	 * @return A {@link Random} {@link Ad}.
	 */
	public static AdNetwork getRandomAdNetwork() {
		String name = randomName();
		AdNetwork adNetwork = new SimpleAdNetwork(name);
		return adNetwork;
	}

	/**
	 * @return A random {@link AdType}.
	 */
	private static String randomName() {
		byte[] name = new byte[AD_NETWORK_NAME_LENGTH];
		random.nextBytes(name);
		int id = Math.abs(random.nextInt());
		return "Ad Network #" + id + " (" + new String(name) + ")";
	}

	/**
	 * @see tau.adx.common.generators.GenericGenerator#generate(int)
	 */
	@Override
	public Collection<AdNetwork> generate(int amount) {
		Collection<AdNetwork> adNetworks = new LinkedList<>();
		for (int i = 0; i < amount; i++) {
			adNetworks.add(getRandomAdNetwork());
		}
		return adNetworks;
	}
}
